# -*- coding: utf8 -*-
from flask_restful import Resource, reqparse
from beidou_code_point import beidou_grid_code_point
from beidou_code_line import beidou_grid_code_line
from beidou_code_polygon import beidou_grid_code_polygon


class BeiDouGridCode2DimensionPoint(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('longitude', required=True, type=str, location='args', help='please provide longitude')
        self.parser.add_argument('latitude', required=True, type=str, location='args', help='please provide latitude')
        self.parser.add_argument('level', required=True, type=int, location='args', help='please provide three_dimension level')

    def get(self):
        args = self.parser.parse_args()
        # 必填请求参数校验
        longitude = args.get('longitude')
        latitude = args.get('latitude')
        level_n = args.get('level')
        try:
            grid_code = beidou_grid_code_point(longitude, latitude, level_n)
            return {"flag": "succeed", "point": (longitude, latitude, level_n), "grid_code": grid_code}, 200
        except:
            # raise
            return {"flag": "fail", "point": (longitude, latitude, level_n), "error": ""}, 400


class BeiDouGridCode2DimensionLine(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('longitudeList', required=True, type=str, location='args',  help='please provide longitudeList')
        self.parser.add_argument('latitudeList', required=True, type=str, location='args', help='please provide latitudeList')
        self.parser.add_argument('level', required=True, type=int, location='args', help='please provide three_dimension level')

    def get(self):
        args = self.parser.parse_args()
        # 必填请求参数校验
        longitudeList = args.get('longitudeList')
        latitudeList = args.get('latitudeList')
        level_n = args.get('level')
        try:
            # print(longitudeList, latitudeList)
            grid_code_list = beidou_grid_code_line(longitudeList, latitudeList, level_n)
            return {"flag": "succeed", "point": (longitudeList, latitudeList, level_n),
                    "grid_code_list": grid_code_list}, 200
        except:
            raise
            return {"flag": "fail", "point": (longitudeList, latitudeList, level_n), "error": ""}, 400


class BeiDouGridCode2DimensionPolygon(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('longitudeList', required=True, type=str, location='args', help='please provide longitudeList')
        self.parser.add_argument('latitudeList', required=True, type=str, location='args', help='please provide latitudeList')
        self.parser.add_argument('level', required=True, type=int, location='args', help='please provide three_dimension level')

    def get(self):
        args = self.parser.parse_args()
        # 必填请求参数校验
        longitudeList = args.get('longitudeList')
        latitudeList = args.get('latitudeList')
        level_n = args.get('level')
        try:
            grid_code_list = beidou_grid_code_polygon(longitudeList, latitudeList, level_n)
            return {"flag": "succeed", "point": (longitudeList, latitudeList, level_n),
                    "grid_code_list": grid_code_list}, 200
        except:
            # raise
            return {"flag": "fail", "point": (longitudeList, latitudeList, level_n), "error": ""}, 400
